package xyz.chenyongpeng.orderconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.entity.*;
import xyz.chenyongpeng.orderconsumerdubbo.service.CouponService;
import xyz.chenyongpeng.orderconsumerdubbo.service.GoodService;
import xyz.chenyongpeng.orderconsumerdubbo.service.UserService;
import xyz.chenyongpeng.to.GoodsTo;
import xyz.chenyongpeng.utils.PagesData;
import xyz.chenyongpeng.utils.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Goods")
public class GoodController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private UserService userService;

    @PostMapping("/list")
    public R list(@RequestParam(name = "gt_id", required = true) String gt_id,
                  @RequestParam(name = "hot_tab", required = false) int hot_tab,
                  @RequestParam(name = "limit", required = false) String limit,
                  @RequestParam(name = "is_recommend", required = false) String is_recommend,
                  @RequestParam(name = "is_hot", required = false) String is_hot) {
        Map<String, Object> parms = new HashMap<>();
        parms.put("status", 1);// 在售状态

        if (!StringUtils.isEmpty(is_recommend)) {
            parms.put("show_label", "推荐");// 是否推荐
        }

        if (!StringUtils.isEmpty(gt_id)) {
            parms.put("show_tab", gt_id);// 商品类型
        }

        if (!StringUtils.isEmpty(is_hot)) {
            parms.put("is_hot", true);
        }

        if (!StringUtils.isEmpty(limit)) {
            parms.put("limit", limit);
        } else {
            parms.put("limit", "0,15");
        }

        List<GoodsTo> goodsTos = goodService.getListByLimit(parms);

        if (goodsTos == null){
            return R.error(200,"没有更多的商品了");
        }
        int count = goodService.getListCount(parms);
        Map<String,Object> result = new HashMap<>();
        result.put("data", new PagesData(count, goodsTos));
        return R.ok(result);
    }

    @PostMapping("/details")
    public R details(@RequestParam(name = "goods_id", required = true) String goods_id,
                     @RequestParam(name = "gt_id", required = false) String gt_id,
                     @RequestParam(name = "wx_openid", required = false) String wx_openid) {
        if (StringUtils.isEmpty(goods_id)) {
            return R.error(500, "参数不全");
        }
        String u_id = userService.getUserIdByOpenid(wx_openid);

        GoodsTo goodsTo = goodService.getOne(goods_id);

        if (goodsTo == null) {
            return R.error(404, "没有这个商品");
        }
        if (goodsTo.getStatus() != 1) {
            return R.error(500, "该商品已经下架");
        }

        if (StringUtils.isEmpty(goodsTo.getAssist_start_time()) && StringUtils.isEmpty(goodsTo.getAssist_end_time()) && !StringUtils.isEmpty(goodsTo.getAssist_msg())) {
            goodsTo.setAssist_msg(goodsTo.getAssist_start_time() + "-" + goodsTo.getAssist_end_time());
        }
        ZsGoodsType zsGoodsType = goodService.getOneType(goodsTo.getGt_id());
        String tabName = zsGoodsType.getTblName();

        Map<String,Object> attr = goodService.getGoodsAttr("zs_"+tabName, goods_id);

//        1	线路游	g_travel_route
//        2	门票	g_tickets
//        3	语音讲解	g_audio
//        4	组合商品套餐	g_combo_attr
//        5	体验游	g_travel_route
        Map<String,Object> result = new HashMap<>();
        switch (goodsTo.getGt_id()) {
            case 1:
                Map<String, Object> data0 = goodService.buildLuxian(goodsTo, attr);
                result.put("data", data0);
                return R.ok(result);
            case 2:
                Map<String, Object> data1 = goodService.buildTickets(goodsTo, attr);
                result.put("data", data1);
                return R.ok(result);
            case 3:
                int r_id = goodsTo.getR_id();
                break;
            case 4:
                Map<String, Object> data4 = goodService.buildCombo(goodsTo, attr);
                result.put("data", data4);
                return R.ok(result);
            case 5:
                Map<String, Object> data5 = goodService.buildLuxian(goodsTo, attr);
                result.put("data", data5);
                return R.ok(result);
            default:
                Map<String, Object> data = goodService.buildOther(goodsTo, attr);
                result.put("data", data);
                return R.ok(result);
        }

        return R.error(500, "服务器内部错误");
    }
}
